   package com.itheima.reggie.controller;

   import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
   import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
   import com.itheima.reggie.common.R;
   import com.itheima.reggie.entity.User;
   import com.itheima.reggie.service.UserService;
   import com.itheima.reggie.utils.emailUtils;
   import lombok.extern.slf4j.Slf4j;


   import javax.servlet.http.HttpSession;

   import org.apache.commons.mail.EmailException;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.web.bind.annotation.*;

   import java.util.Map;

   @RestController
   @RequestMapping("/user")
   @Slf4j
   public class UserController {
       @Autowired
       private UserService userService;

       /**
        * 发送QQ邮箱验证码
        *
        * @param user
        * @param session
        * @return
        */
       @PostMapping("/sendMsg")
       public R<String> sendMsg(@RequestBody User user, HttpSession session) throws EmailException {
           String phone = user.getPhone();
           if (!phone.isEmpty()) {
               //随机生成一个验证码
               String code = emailUtils.achieveCode();
               log.info(code);
               //这里的phone其实就是邮箱，code是我们生成的验证码
               emailUtils.sendAuthCodeEmail(phone, code);
               //验证码存session，方便后面拿出来比对
               session.setAttribute(phone, code);
               return R.success("验证码发送成功");
           }
           return R.error("验证码发送失败");
       }

       /**
        * 移动端用户登录
        *
        * @param map
        * @param session
        * @return
        */
       @PostMapping("/login")
       public R<User> login(@RequestBody Map map, HttpSession session) {
           log.info(map.toString());
           //获取邮箱
           String phone = map.get("phone").toString();
           //获取验证码
           String code = map.get("code").toString();
          //从这里开始注释 //从session中获取验证码
           String codeInSession = session.getAttribute(phone).toString();
           //比较这用户输入的验证码和session中存的验证码是否一致
           if (code != null && code.equals(codeInSession)) {
      /*         if (true) {*/
                   //如果输入正确，判断一下当前用户是否存在
                   LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
                   //判断依据是从数据库中查询是否有其邮箱
                   queryWrapper.eq(User::getPhone, phone);
                   User user = userService.getOne(queryWrapper);
                   //如果不存在，则创建一个，存入数据库
                   if (user == null) {
                       user = new User();
                       user.setPhone(phone);
                       user.setName("用户" + codeInSession);
                       user.setStatus(1);
                       userService.save(user);
                   }
                   //存个session，表示登录状态
                   session.setAttribute("user", user.getId());
                   //并将其作为结果返回
                   return R.success(user);
               }
               return R.error("登录失败");
           }

       /**
        * 退出登录
        * @param session
        * @return
        */
           @PostMapping("/loginout")
            public R<String> loginout(HttpSession session) {
                session.removeAttribute("user");
                return R.success("退出成功");
            }


       }
